# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
#

# Makefile for building omnkms.ko against the running kernel

# Module name (without .ko)
obj-m := omnkms.o

omnkms-objs := omnkms_drv.o omnkms_plane.o \
	omnkms_output.o \
	omnkms_formats.o \
	omnkms_crtc.o \
	omnkms_composer.o \
	omnkms_writeback.o \
	omnkms_ctrl.o

# Path to the currently running kernel's build system
KDIR := /lib/modules/$(shell uname -r)/build

# Current directory (where omnkms.c is located)
PWD := $(shell pwd)

# Detect kernel version and set flags for older kernel API
KERNEL_RELEASE := $(shell uname -r)
ifeq ($(findstring el10_0, $(KERNEL_RELEASE)),el10_0)
  ccflags-y += -DEL10_0
endif

# Feature probe: hrtimer_setup() was introduced upstream in v6.13 and
# hrtimer_init() was removed in v6.15. RHEL backports both changes without
# bumping LINUX_VERSION_CODE, so we must grep the kernel headers directly
# rather than rely on a version check.
HAVE_HRTIMER_SETUP := $(shell grep -q "hrtimer_setup" $(KDIR)/include/linux/hrtimer.h 2>/dev/null && echo 1)
ifeq ($(HAVE_HRTIMER_SETUP),1)
  ccflags-y += -DHAVE_HRTIMER_SETUP
endif

all:
	$(MAKE) -C $(KDIR) M=$(PWD) modules

test:
	gcc -o omnkms_test omnkms_test.c -I$(PWD) -I/usr/include/drm -ldrm

capture_test:
	gcc -Wall -Wextra -o omnkms_capture_test omnkms_capture_test.c -I$(PWD)

cursor_test:
	gcc -Wall -Wextra -o omnkms_cursor_test omnkms_cursor_test.c -I$(PWD)

clean:
	$(MAKE) -C $(KDIR) M=$(PWD) clean
	rm -f omnkms_test omnkms_capture_test omnkms_cursor_test

